package com.itheima.mapper;

import com.itheima.entity.Article;
import com.itheima.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface IndexMapper {

    /**
     * 查找所有分类数据 ydd_cate
     * @return
     */
    @Select("select id,catename,sort_num num from ydd_cate")
    List<CateVO> selectAllCate();

    /**
     * 广告位总数
     * @return
     */
    @Select("select COUNT(*) from ydd_advpos")
    Integer selectNumber();

    /**
     * 标签总数
     * @return
     */
    @Select("select COUNT(*) from ydd_tag")
    Integer selectTagNumber();

    /**
     * 管理员总数
     * @return
     */
    @Select("select COUNT(*) from ydd_user_data group by user_id having user_id = 136")
    Integer selectAdminNumber();

    /**
     * 文章总数
     * @return
     */
    @Select("select COUNT(*) from ydd_article")
    Integer selectArticleNumber();

    /**
     * 分类总数
     * @return
     */
    @Select("select COUNT(*) from ydd_cate")
    Integer selectCateNumber();

    @Select("select COUNT(*) from ydd_advimg")
    Integer selectImgNumber();

    /**
     * 查询最新用户数据
     * @return
     */
    @Select("select id,username,sex,icon,address from ydd_user")
    List<YddUserVO> selectUser();

    @Select("select id,title,author,keywords,click from ydd_article where ishot = 1")
    List<YddArticleVO> selectHotArticleData();

    @Select("select COUNT(*) from ydd_user group by sex")
    List<Long> selectUserNumber();

    /**
     * 获取登录配置信息
     * @return
     */
//    @Select("select attr_value from sys_config where attr_key in (#{login_title},#{login_bg},#{system_name},#{system_logo},#{web_ico})")
    /*@Select("select attr_value from sys_config where attr_key in ('login_title','login_bg','system_name','system_logo','web_ico')")
    ConfigVO selectLoginConfig();*/
    @Select("select attr_key,attr_value from sys_config where attr_key in ('login_title','login_bg','system_name','system_logo','web_ico')")
    List<Map<String,String>> selectLoginConfig();


    /**
     * 根据id拿到所有信息
     *
     * @param id
     * @return {@link Article}
     */
    @Select("select * from ydd_article where id = #{id}")
    Article selectById(Long id);

    /**
     * 上一篇数据
     *
     * @param id
     * @return {@link List}<{@link ShowVO}>
     */
    @Select("SELECT id,title FROM ydd_article WHERE ydd_article.id < #{id} ORDER BY id ASC LIMIT 1")
    List<ShowVO> selectIDAndTitleByID(Long id);


    /**
     * 下一篇数据
     *
     * @param id
     * @return {@link List}<{@link ShowVO}>
     */
    @Select("SELECT id,title FROM ydd_article WHERE ydd_article.id > #{id} ORDER BY id ASC LIMIT 1")
    List<ShowVO> selectNextIDAndTitleByID(Long id);

    /**
     * 查找分类名
     *
     * @param cateid
     * @return {@link String}
     */
    @Select("select catename from ydd_cate where id = #{cateid}")
    String selectCatename(Integer cateid);

    /**
     * 查找推荐文章
     *
     * @return {@link List}<{@link Article}>
     */
    @Select("select * from ydd_article where ishot = 1")
    List<Article> selectByIshot();

    /**
     * 原始查询---根据分类id查询
     *
     * @param cateid
     * @return {@link List}<{@link YddArticleVO2}>
     */
    @Select("select * from ydd_article where cateid = #{cateid}")
    List<Article> selectByCateId(Integer cateid);


    /**
     * 推荐文章
     *
     * @return {@link List}<{@link Article}>
     */
    @Select("select * from ydd_article where ishot = 1 and istop = 1")
    List<Article> selectRecommend();

    /**
     * 热门文章
     *
     * @return {@link List}<{@link Article}>
     */
    @Select("select * from ydd_article where create_date > 1577515400000")
    List<Article> selectHot();

    /**
     * 24小时最新文章数量
     *
     * @param previousTimestamp
     * @param timestamp
     * @return {@link Integer}
     */
    @Select("select COUNT(*) from ydd_article where create_date > #{previousTimestamp} and create_date < #{timestamp}")
    Integer selectCountByCreateDate(long previousTimestamp, long timestamp);

    /**
     * 热门置顶文章
     *
     * @return {@link List}<{@link Article}>
     */
    @Select("select * from ydd_article where ishot = 1 and istop = 1")
    List<Article> selectByHotAndTop();

    /**
     * 所有友情链接（文字和图片）
     *
     * @return {@link List}<{@link LinkVO}>
     */
    @Select("select id, title, url, des, img, type from ydd_link")
    List<LinkVO> selectAllLink();

    /**
     * 轮播图数据
     *
     * @param bannerId
     * @return {@link List}<{@link BannerVO}>
     */
    @Select("select id, advimglink, advimgdesc, advimgsrc from ydd_advimg where id = #{bannerId}")
    List<BannerVO> selectBannerById(Integer bannerId);


    /**
     * 详情广告图
     *
     * @param showBannerIds
     * @return {@link List}<{@link BannerVO}>
     */
    List<BannerVO> selectShowBannerById(List<Integer> showBannerIds);
}
